{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing packages\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import sklearn\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn import preprocessing\n",
    "import statsmodels.api as sm\n",
    "import seaborn as sns; sns.set(color_codes=True)\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Experiment Parameters (v=10,lam=.125,c=1,T=10)\n",
    "prizelist=[10]\n",
    "v=10\n",
    "lam=.125\n",
    "c=1\n",
    "T=10\n",
    "N=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lower_cdf(x,T):\n",
    "    return (1-np.exp(-lam*x))**T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def upper_cdf(x,z,T):\n",
    "    cdf= ((1-np.exp(-lam*z))**T) + (1-((1-np.exp(-lam*z))**T))*float((1-np.exp(-lam*x))-(1-np.exp(-lam*z)))/(1-(1-np.exp(-lam*z)))\n",
    "    return cdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cdf(x,z,T):\n",
    "    if x<=z:\n",
    "        return lower_cdf(x,T)\n",
    "    elif x>z:\n",
    "        return upper_cdf(x,z,T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def exponential_pdf(value):\n",
    "    return lam*np.exp(-lam*(value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Cutoff Testing**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "threshList=[]\n",
    "dx=.1\n",
    "\n",
    "for N in range(2,3):\n",
    "    z=0\n",
    "    v=prizelist[0]\n",
    "    value=v*sum([((cdf(z+dx*x,z,T)**(N-1))-(cdf(z,z,T)**(N-1)))*exponential_pdf(z+dx*x)*dx for x in range(1000*round(1/dx))])-c\n",
    "    while value>0:\n",
    "        z=z+.01\n",
    "        value=v*sum([((cdf(z+dx*x,z,T)**(N-1))-(cdf(z,z,T)**(N-1)))*exponential_pdf(z+dx*x)*dx for x in range(1000*round(1/dx))])-c\n",
    "    threshList.append(z)\n",
    "    print(N,z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cutoff(iterations,N):\n",
    "    for i in range(iterations):\n",
    "        z=i*.01\n",
    "        if v*sum([(cdf(z+dx*x,z,T)**(N-1)-cdf(z,z,T)**(N-1))*exponential_pdf(z+dx*x)*dx for x in range(100*round(1/dx))])-c<0:\n",
    "            break\n",
    "    z=z-.01\n",
    "    return z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dx=.1\n",
    "z=cutoff(10000,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p2BelowF=[]\n",
    "p2AboveF=[]\n",
    "\n",
    "p6BelowF=[]\n",
    "p6AboveF=[]\n",
    "\n",
    "p10BelowF=[]\n",
    "p10AboveF=[]\n",
    "\n",
    "p2BelowL=[]\n",
    "p2AboveL=[]\n",
    "\n",
    "p6BelowL=[]\n",
    "p6AboveL=[]\n",
    "\n",
    "p10BelowL=[]\n",
    "p10AboveL=[]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "z=threshList[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "drawslist=[]\n",
    "scoreslist=[]\n",
    "for iter in range(1000000):\n",
    "    scores=[0 for i in range(N)]\n",
    "    draws=[0 for i in range(N)]\n",
    "    for period in range(1,T+1):\n",
    "        for player in range(N):\n",
    "            FS=int(round((1-np.exp(-.125*scores[player])*1000)))\n",
    "            mscore=scores[player]\n",
    "            if period==1:\n",
    "                if mscore<=z:\n",
    "                    scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                    draws[player]=draws[player]+1\n",
    "            if period>1:\n",
    "                if leader[player]==1:\n",
    "                    choice=0\n",
    "                    if mscore<=z:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                        choice=1\n",
    "                        \n",
    "                    if (period==2)&(mscore<=15):\n",
    "                        p2BelowL.append(choice)\n",
    "                    if (period==2)&(mscore>15):\n",
    "                        p2AboveL.append(choice)\n",
    "                    if (period==6)&(mscore<=15):\n",
    "                        p6BelowL.append(choice)\n",
    "                    if (period==6)&(mscore>15):\n",
    "                        p6AboveL.append(choice)\n",
    "                    if (period==10)&(mscore<=15):\n",
    "                        p10BelowL.append(choice)\n",
    "                    if (period==10)&(mscore>15):\n",
    "                        p10AboveL.append(choice)\n",
    "                    \n",
    "                elif leader[player]==0:\n",
    "                    choice=0\n",
    "                    if mscore<=z:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                        choice=1\n",
    "                        \n",
    "                    if (period==2)&(mscore<=15):\n",
    "                        p2BelowF.append(choice)\n",
    "                    if (period==2)&(mscore>15):\n",
    "                        p2AboveF.append(choice)\n",
    "                    if (period==6)&(mscore<=15):\n",
    "                        p6BelowF.append(choice)\n",
    "                    if (period==6)&(mscore>15):\n",
    "                        p6AboveF.append(choice)\n",
    "                    if (period==10)&(mscore<=15):\n",
    "                        p10BelowF.append(choice)\n",
    "                    if (period==10)&(mscore>15):\n",
    "                        p10AboveF.append(choice)\n",
    "                    \n",
    "        leader=[0 for i in range(N)]\n",
    "        leader[np.argmax(scores)]=1\n",
    "#             print(leader)\n",
    "#     print(draws)\n",
    "    drawslist.append(sum(draws))\n",
    "    scoreslist.append(max(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.array(drawslist).mean(),\"\\n\")\n",
    "print(np.array(p2BelowL).mean(),np.array(p6BelowL).mean(),np.array(p10BelowL).mean(),\"\\n\")\n",
    "print(np.array(p2AboveL).mean(),np.array(p6AboveL).mean(),np.array(p10AboveL).mean(),\"\\n\")\n",
    "print(np.array(p2BelowF).mean(),np.array(p6BelowF).mean(),np.array(p10BelowF).mean(),\"\\n\")\n",
    "print(np.array(p2AboveF).mean(),np.array(p6AboveF).mean(),np.array(p10AboveF).mean(),\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
